package st.business.tepth.business.user.busnumber.workholder.maintain.work.content.standardwork.workdetail;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.DiagnoseBean;
import st.business.tepth.business.bean.MaintainWorkDetailBean;
import st.business.tepth.business.bean.PartLocationBean;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.user.busnumber.workholder.adapter.TroubleProgramAdapter;
import st.business.tepth.business.user.busnumber.workholder.repair.work.content.RepairDataConvert;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.base.mvp.MvpFragment;
import st.shadow.library.ui.dialog.DialogLoader;
import st.shadow.library.ui.recycler.BaseDecoration;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-04-15 15:34
 * 保养详情
 */

public class WorkDetailReadOnlyDelegate extends MvpFragment<WorkDetailPresenter>
        implements WorkDetailContract.View {
    public static WorkDetailReadOnlyDelegate newInstance(int maintainId,
                                                         String person) {
        Bundle bundle = new Bundle();
        bundle.putInt("maintainId", maintainId);
        bundle.putString("person", person);
        WorkDetailReadOnlyDelegate delegate = new WorkDetailReadOnlyDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.tv_standard_work)
    TextView mTvStandardWork;
    @BindView(R2.id.tv_work_person)
    TextView mTvWorkPerson;
    @BindView(R2.id.recycler_view_diagnose)
    RecyclerView mRecyclerViewDiagnose;
    @BindView(R2.id.recycler_view_program)
    RecyclerView mRecyclerViewProgram;
    @BindView(R2.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R2.id.tv_diagnose)
    TextView mTvDiagnose;
    @BindView(R2.id.tv_add_work)
    TextView mTvAddWork;
    @BindView(R2.id.tv_commit_value)
    TextView mTvCommitValue;
    @BindView(R2.id.diver_3)
    View vDiver3;
    @BindView(R2.id.v_value)
    TextView mVvalue;
    @BindView(R2.id.recycler_view_value)
    RecyclerView mRvValue;
    private DiagnoseAdapter mDiagnoseAdapter;
    private TroubleProgramAdapter mProgramAdapter;
    private int mMaintainId;
    private String mPerson;
    private DialogLoader mDialogLoader;
    private UiDelegate mUiDelegate;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mMaintainId = bundle.getInt("maintainId");
            mPerson = bundle.getString("person");
        }
    }

    @Override
    public WorkDetailPresenter getPresenter() {
        return new WorkDetailPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_maintain_work_detail;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        initView();
        initData();
    }

    private void initView() {
        mTvWorkPerson.setText(mPerson);
        mDialogLoader = new DialogLoader(_mActivity);
        mRecyclerViewDiagnose.setLayoutManager(
                new LinearLayoutManager(_mActivity, LinearLayoutManager.HORIZONTAL
                        , false));
        mRecyclerViewDiagnose.setAdapter(mDiagnoseAdapter =
                new DiagnoseAdapter(R.layout.item_maintain_diagnose, 6, null));
        mRecyclerViewDiagnose.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter,
                                               View view, final int position) {
            }
        });
        initProgramView();
    }

    private void initProgramView() {
        mRecyclerViewProgram.setLayoutManager(new LinearLayoutManager(_mActivity));
        mRecyclerViewProgram.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            }
        });
        mProgramAdapter = new TroubleProgramAdapter(_mActivity, null, null
                , null, null);
        mProgramAdapter.addHeaderView(LayoutInflater.from(_mActivity).inflate(R.layout.item_head_work_program,
                null));
        mRecyclerViewProgram.setAdapter(mProgramAdapter);
    }

    private void initData() {
        mUiDelegate = new UiDelegate(mTvAddWork, mTvDiagnose, mRecyclerViewDiagnose,
                mDiagnoseAdapter, mRecyclerViewProgram,
                mProgramAdapter, mTvConfirm, _mActivity);
        mUiDelegate.initState(6);
        //请求界面数据
        mPresenter.getDetailData(UserManager.getInstance().getToken(), mMaintainId);
        mDialogLoader.showProgressDialog(true, "正在请求", "请稍候...");
    }

    @Override
    public void showData(MaintainWorkDetailBean workDetailBean) {
        mDialogLoader.clearDialog();
        if (workDetailBean != null) {
            if (workDetailBean.getDisplayMeasured() == 0) {
                //不需要显示实测值
                mRvValue.setVisibility(View.GONE);
                mTvCommitValue.setVisibility(View.GONE);
                vDiver3.setVisibility(View.GONE);
                mVvalue.setVisibility(View.GONE);
            } else {
                mRvValue.setVisibility(View.VISIBLE);
                vDiver3.setVisibility(View.VISIBLE);
                mVvalue.setVisibility(View.VISIBLE);
                //可作业
                List<MaintainWorkDetailBean.MeasureValue>
                        valueList = workDetailBean.getMeasuredValueList();
                if (valueList != null && !valueList.isEmpty()) {
                    mRvValue.setLayoutManager(new LinearLayoutManager(_mActivity,
                            LinearLayoutManager.HORIZONTAL, false));
                    mRvValue.addItemDecoration(BaseDecoration.
                            create(Color.parseColor("#ffffff"), 5));
                    mRvValue.setAdapter(new BaseQuickAdapter<MaintainWorkDetailBean.MeasureValue,
                            BaseViewHolder>(R.layout.item_measure_value, valueList) {
                        @Override
                        protected void convert(final BaseViewHolder helper,
                                               final MaintainWorkDetailBean.MeasureValue item) {
                            helper.setText(R.id.tv_value, item.getMeasuredValue());
                            String unit = " ";
                            if (item.getUnit() != null && !"null".equals(item.getUnit())) {
                                unit = item.getUnit();
                            }
                            helper.setText(R.id.tv_unit, unit);
                            helper.setEnabled(R.id.view_cover, false);
                            helper.getView(R.id.view_cover).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    new DialogInputValue(_mActivity, new OnItemChooseListener<String>() {
                                        @Override
                                        public void onChoose(String s) {
                                            item.setMeasuredValue(s);
                                            helper.setText(R.id.tv_value, s);
                                        }
                                    }).show();
                                }
                            });

                        }
                    });
                }
                //不可作业
                mTvCommitValue.setVisibility(View.GONE);
            }
            mTvStandardWork.setText(workDetailBean.getStandard());
            List<DiagnoseBean> diagnoseBeans = workDetailBean.getRepairDiagnosticList();
            mDiagnoseAdapter.setNewData(diagnoseBeans);
            if (diagnoseBeans != null) {
                if (mProgramAdapter != null) {
                    mProgramAdapter.setNewData(
                            new RepairDataConvert()
                                    .programToMultipleItemEntityInMaintainDetail(diagnoseBeans, 6));
                }
            }
            mUiDelegate.changeStateWithData();
            mUiDelegate.initState(6);
        }
    }

    @Override
    public void showError(String msg) {
        mDialogLoader.stopProgressWithFailed(msg, msg);
    }

    @Override
    public void postDiagnoseList(List<DiagnoseBean> list) {

    }

    @Override
    public void deleteDiagnose(final int position) {

    }

    @Override
    public void deleteProgram(final int position) {

    }

    @Override
    public void returnGoodsSucceed(int position) {

    }

    @Override
    public void showPickGoods(List<PickGoods> list) {

    }

    @Override
    public void pickGoodsSucceed() {

    }

    @Override
    public void commitValueSucceed() {

    }

    @Override
    public void showTempLocation(PartLocationBean partLocationBean, int position) {

    }

    @OnClick({R2.id.tv_back})
    public void onBack() {
        pop();
    }
}